#include <iostream>
#include <cstdlib>
using namespace std;

#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
struct LNode
{
	ElemType data;
	struct LNode *next;
};
typedef LNode *LinkList;

Status InitList(LinkList &L);
Status DestroyList(LinkList &L);
Status ClearList(LinkList L);
Status ListEmpty(LinkList L);
int ListLength(LinkList L);
Status GetElem(LinkList L,int i,ElemType &e);
int LocateElem(LinkList L,ElemType e);
Status PriorElem(LinkList L,ElemType cur_e,ElemType &pre_e);
Status NextElem(LinkList L,ElemType cur_e,ElemType &next_e);
Status ListInsert(LinkList L,int i,ElemType e);
Status ListDelete(LinkList L,int i,ElemType &e);
Status ListTraverse(LinkList L);

int main()
{
	Status s;
	LinkList La,Lb,Lc;
	InitList(La);
	InitList(Lb);
	s=ListInsert(La,1,3);
	s=ListInsert(La,2,5);
	s=ListInsert(La,3,8);
	s=ListInsert(La,4,11);

	s=ListInsert(Lb,1,2);
	s=ListInsert(Lb,2,6);
	s=ListInsert(Lb,3,8);
	s=ListInsert(Lb,4,9);
	s=ListInsert(Lb,5,11);
	s=ListInsert(Lb,6,15);
	s=ListInsert(Lb,7,20);

	
	s=ListTraverse(La);
	s=ListTraverse(Lb);

	
	return 0;
}
Status InitList(LinkList &L)
{
	L=(LinkList)malloc(sizeof(LNode));
	if(!L)
		exit(-1);
	L->next=NULL;
	return TRUE;
}
Status DestroyList(LinkList &L)
{
	LinkList p=L,q;
	while(p)
	{
		q=p->next;
		free(p);
		p=q;
	}
	return TRUE;
}
Status ClearList(LinkList L)
{
	LinkList p=L->next,q;
	while(p)
	{
		q=p->next;
		free(p);
		p=q;
	}
	L->next=NULL;
	return TRUE;
}
Status ListEmpty(LinkList L)
{
	if(L->next)
		return FALSE;
	else
		return TRUE;
}
int ListLength(LinkList L)
{
	int i=0;
	LinkList p=L->next;
	while(p)
	{
		p=p->next;
		i++;
	}
	return i;

}
Status GetElem(LinkList L,int i,ElemType &e)
{
	int j=1;
	LinkList p=L->next;
	while(p && j<i)
	{
		p=p->next;
		j++;
	}
	if(!p || j>i)
		return FALSE;
	e=p->data;
	return TRUE;
}
int LocateElem(LinkList L,ElemType e)
{
	int i=1;
	LinkList p=L->next ;
	while(p)
	{
		if(p->data==e)
			return i;
		p=p->next ;
		i++;
	}
	return 0;
}
Status PriorElem(LinkList L,ElemType cur_e,ElemType &pre_e)
{
	LinkList q,p=L->next ;
	while(p->next)
	{
		q=p->next;
		if(q->data==cur_e)
		{
			pre_e=p->data;
			return TRUE;
		}
		p=q;
	}
	return FALSE;
}
Status NextElem(LinkList L,ElemType cur_e,ElemType &next_e)
{
	LinkList p=L->next;
	while(p->next )
	{
		if(p->data ==cur_e)
		{
			next_e=p->next->data;
			return TRUE;
		}
		p=p->next;
	}
	return FALSE;
}
Status ListInsert(LinkList L,int i,ElemType e)
{
	LinkList p=L,q;
	int j=0;
	while(p && j<i-1)
	{
		p=p->next;
		j++;
	}
	if(!p || j>i-1)
		return FALSE;
	q=(LinkList)malloc(sizeof(LNode));
	if(!q)
		exit(-1);
	q->data=e;
	q->next=p->next;
	p->next=q;
	return TRUE;
}
Status ListDelete(LinkList L,int i,ElemType &e)
{
	LinkList p=L ,q;
	int j=0;
	while(p->next && j<i-1)
	{
		p=p->next;
		j++;
	}
	if(!p->next  || j>i-1)
		return FALSE;
	q=p->next;
	p->next=q->next ;
	e=q->data;
	free(q);
	return TRUE;
}
Status ListTraverse(LinkList L)
{
	LinkList p=L->next;
	while(p)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
	return TRUE;
}
